package com.gzy.fisherybackend.controller;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.gzy.fisherybackend.common.BaseResponse;
import com.gzy.fisherybackend.common.ErrorCode;
import com.gzy.fisherybackend.exception.BusinessException;
import com.gzy.fisherybackend.utils.AliOSSUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliOSSUtils aliOSSUtils;

    //上蹿到阿里云的OSS
    @PostMapping("/upload")
    public BaseResponse<String> upload(@RequestParam("image") MultipartFile image) throws IOException {
        if (image == null || image.isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "上传文件不能为空");
        }
        String url = aliOSSUtils.upload(image);
        return BaseResponse.success(url);
    }
}
